﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace Travel
{
    class FlightProxy
    {
        internal static List<eFlight> GetFlights(string departureCity, string arrivalCity, DateTime departureDate)
        {
            const string CODE = "Codigo";
            const string DESCRIPTION = "Descriptor";
            const string ARRIVAL = "Llegada";

            List<eFlight> flights = new List<eFlight>();
            XmlDocument xml = new XmlDocument();
            using (AirTicketReference.AirTicketsSoapClient airTickets = new AirTicketReference.AirTicketsSoapClient())
            {
                try
                {
                    xml.LoadXml(airTickets.GetAvailableFlights(departureCity, arrivalCity, departureDate));

                    foreach (XmlElement node in xml.SelectNodes("//Vuelos/Vuelo"))
                    {
                        flights.Add(new eFlight
                        {
                            Code = node.SelectSingleNode(CODE).InnerText,
                            Descriptor = node.SelectSingleNode(DESCRIPTION).InnerText,
                            Arrival = DateTime.Parse( node.SelectSingleNode(ARRIVAL).InnerText)
                        });

                    }
                    return flights;

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return null;
                }

            }
        }

        internal static List<eSeat> GetSeats(string flightCode)
        {
            List<eSeat> seats = new List<eSeat>();

            XmlDocument xml = new XmlDocument();
            try
            {
                using (AirTicketReference.AirTicketsSoapClient airTickets = new AirTicketReference.AirTicketsSoapClient())
                {
                    xml.LoadXml(airTickets.GetAvailableSeats(flightCode));

                    foreach (XmlElement node in xml.SelectNodes("//Asientos/Asiento"))
                    {
                        seats.Add(new eSeat
                        {
                            Descriptor = node.InnerText
                        });

                    }
                    return seats;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }


        internal static string GetTicket(string flightCode, string seatCode, string Passenger)
        {
            
            XmlDocument xml = new XmlDocument();
            try
            {
                using (AirTicketReference.AirTicketsSoapClient airTickets = new AirTicketReference.AirTicketsSoapClient())
                {
                    xml.LoadXml(airTickets.BookSeatInFligh(flightCode, seatCode));
                    
                    if (xml.SelectSingleNode("//Error") != null)
                    {
                        throw new Exception(xml.SelectSingleNode("//Error").InnerText);
                    }
                    XmlElement node = (XmlElement)xml.SelectSingleNode("//Billete");
                    return node.InnerText;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }
    }
}

